﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/12/1
 * 时间: 15:41
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using System.IO;
using System.Reflection; 

namespace QRCode
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
			InitializeComponent();
			
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			int x=int.Parse(iniHelper.Read("QRCode","left",inipath));
			int y=int.Parse(iniHelper.Read("QRCode","top",inipath));
			this.Location=new Point(x,y);
			 
			int w=int.Parse(iniHelper.Read("QRCode","width",inipath));
			int h=int.Parse(iniHelper.Read("QRCode","height",inipath));
			this.Size=new Size(w,h);
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            //默认命名空间+文件夹名+.[注意]
            string resourceName = "QRCode." + new AssemblyName(args.Name).Name + ".dll";

            // LogHelper.Debug("在加载资源：" + resourceName);
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                if (stream != null)
                {
                    byte[] assemblyData = new byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }

                return null;
            }
        }
		
		/// <summary>
          /// 生成二维码图片
          /// </summary>
          /// <param name="strMessage">要生成二维码的字符串</param>
          /// <param name="width">二维码图片宽度</param>
          /// <param name="height">二维码图片高度</param>
         /// <returns></returns>
         private Bitmap GetQRCodeByZXingNet(String strMessage,Int32 width,Int32 height)
          {
             Bitmap result = null;
             try
             {
                 BarcodeWriter barCodeWriter = new BarcodeWriter();
                 barCodeWriter.Format = BarcodeFormat.QR_CODE;
                 barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                 barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
                 barCodeWriter.Options.Height = height;
                 barCodeWriter.Options.Width = width;
                 barCodeWriter.Options.Margin = 0;
                 ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
                 result = barCodeWriter.Write(bm);
             }
             catch (Exception ex)
             {
                 //异常输出
             }
             return result;
         }
		void PictureBox1MouseClick(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left) {//左键
				Form f=new Form();
				PictureBox pic=new PictureBox();
				f.Controls.Add(pic);
				pic.Image=GetQRCodeByZXingNet(Clipboard.GetText(), 200, 200);
				pic.Width=200;
				pic.Height=200;
				f.Width=210;
				f.Height=235;
				string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
				int x=int.Parse(iniHelper.Read("QRCode","left",inipath));
				f.StartPosition = FormStartPosition.Manual;
				f.Left=x;
				f.Top=40;
				f.Show();
				//GetQRCodeByZXingNet(Clipboard.GetText(), 200, 200).Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)+"\\二维码.png");
			} 
			//右键
    		else if(e.Button == MouseButtons.Right){
				Clipboard.SetImage(GetQRCodeByZXingNet(Clipboard.GetText(), 200, 200));
			} 
		}
		void Timer1Tick(object sender, EventArgs e)
		{
			string inipath=Directory.GetCurrentDirectory()+"\\setting.ini"; 
			string color=iniHelper.Read("Topbar","backcolor",inipath);
			this.BackColor =ColorTranslator.FromHtml(color); 
		}
		 
         
         
	}
}
